/*******************************************************************************
*  file    : archivedb.hpp
*  created : 19.08.2013
*  author  : 
*******************************************************************************/

#ifndef ARCHIVEDB_HPP
#define ARCHIVEDB_HPP

#include <QObject>
#include <QString>
#include <vector>
#include <array>

#define MOLES_FIELDS 6
#define MOLES_ID     0
#define MOLES_PHONE  1
#define MOLES_NAME   2
#define MOLES_PASSWD 3
#define MOLES_MEMO   4
#define MOLES_BAT_CHANGE 5

#define NO_SND_FIELDS    4
#define NO_SND_ID        0
#define NO_SND_RECIPIENT 1
#define NO_SND_TEXT      2
#define NO_SND_STATE     3

#define TASKS_FIELDS     6
#define TASKS_ID         0
#define TASKS_REQUEST    1
#define TASKS_ANSWER     2
#define TASKS_STARTDATE  3
#define TASKS_DONEDATE   4
#define TASKS_STATE      5

#define INCOMING_FIELDS  4
#define INCOMING_ID      0
#define INCOMING_TEXT    1
#define INCOMING_SOURCE  2
#define INCOMING_DATE    3

typedef std::array<QString,MOLES_FIELDS >   MoleLine;
typedef std::array<QString,NO_SND_FIELDS>   OutgoingLine;
typedef std::array<QString,TASKS_FIELDS>    TasksLine;
typedef std::array<QString,INCOMING_FIELDS> IncomingLine;

class ArchiveDB : public QObject
{
    Q_OBJECT
public:
    static ArchiveDB*         instance        ();
    bool                      connected       () const;
    const QString&            databaseName    () const;
    void                      setDatabaseName (const QString& name);
public:
    bool                      addMole         (const QString& phone,
                                               const QString& name ,
                                               const QString &passwd,
                                               const QString& memo,
                                               const QString& bat_ch);
    bool                      updateMole      (const QString& phone,
                                               const QString& name ,
                                               const QString &passwd,
                                               const QString& memo,
                                               const QString& bat_ch, int id);
    std::vector<MoleLine>&    getMoles        ();
    bool                      hideMoles       (const QStringList& id_list);
    std::vector<OutgoingLine> getNonSended    ();
    void                      setOutgoingState(const QString& id, int state);
    bool                      addSmsToOutgoing(int, const QString& );
    bool                      saveIncomingSms (const QString& text,
                                               const QString& from,
                                               const QString& data);
    void                      setTaskState    (int task_id, int state);
    void                      setTaskDone     (int task_id, int answer_id );
    std::vector<TasksLine>    getTasks        (int status);
    std::vector<OutgoingLine> getSended       (int from, int till);
    std::vector<IncomingLine> getIncoming     ();
    IncomingLine              getLastIncomingWithText (int num_id,const QString& txt);
    std::vector<IncomingLine> getLastIncomingListWithText(const QStringList& nums,
                                                          const QString &txt);
private:
    explicit ArchiveDB(QObject *parent = 0);
    void                      connectToDB     ();
    bool                      makeDB          ();
public slots:
    void                      start           ();
private slots:
    void                      stop            ();
private:
    QString data_name_;
    std::vector<MoleLine> mole_cache_;
    bool is_mole_cached_;
    //TODO: cache tasks;
    std::vector<TasksLine> task_cache_;
    bool is_task_cached_;
};

#endif // ARCHIVEDB_HPP
